package com.ysg.base;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;

import com.google.android.material.bottomsheet.BottomSheetDialog;

/**
 * dialog：常用方法：setCanceledOnTouchOutside()-点击布局之外是否可以消失
 * setWidthScale():布局宽度
 * setAlpha():布局背景透明度
 */
public abstract class BaseSheetDialog extends BottomSheetDialog {
    public Context mContext;
    protected float widthScale = 0.8f;
    protected float alpha = 1f;
    protected boolean isSquare = false;

    public BaseSheetDialog(@NonNull Context context) {
        super(context);
        this.mContext = context;
    }

    public BaseSheetDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        this.mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        initView();
        initWidthScale();
        initData();
        initListener();
    }

    public abstract int getLayoutId();

    public void initWidthScale(){
        //去掉默认圆角
        Window window = getWindow();
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        WindowManager windowManager = getWindow().getWindowManager();
        int screenWidth = windowManager.getDefaultDisplay().getWidth();
        WindowManager.LayoutParams attributes = getWindow().getAttributes();
        attributes.alpha = alpha;
        attributes.width = (int)(screenWidth * widthScale);
        if (isSquare){
            attributes.height = (int)(screenWidth * widthScale);
        }
        getWindow().setAttributes(attributes);
    }

    public void setAlpha(float alpha) {
        this.alpha = alpha;
    }

    public void setWidthScale(float widthScale){
        this.widthScale = widthScale;
    }

    public void setSquare(boolean square) {
        isSquare = square;
    }

    public abstract void initView();
    public abstract void initData();
    public abstract void initListener();
}
